package com.autumn.swagger.configuration;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;

import org.springframework.core.annotation.AnnotationAttributes;

import com.autumn.swagger.annotation.ApiGroup;
import com.autumn.swagger.annotation.ApiHeaderParameter;
import com.autumn.swagger.annotation.EnableAutumnSwagger;

/**
 * 属性配置工厂
 *
 * @author xinghua
 * @date 2018/12/21
 * @since 1.0.0
 */
class SwaggerInitializer {

	/**
	 * 文档信息
	 */
	static AutumnApiInfo SWAGGER_API_INFO = null;

	/**
	 * 构建 API 分组信息
	 *
	 * @param annotationAttributes
	 *            注解属性
	 * @return API 分组信息
	 */
	private static List<AutumnApiHeaderParameterInfo> buildHeaderParameters(AnnotationAttributes annotationAttributes) {
		List<AutumnApiHeaderParameterInfo> list = new ArrayList<>();
		AnnotationAttributes[] groupAttributes = annotationAttributes
				.getAnnotationArray(EnableAutumnSwagger.FILED_HEADER_PARAMETERS);
		if (groupAttributes != null && groupAttributes.length > 0) {
			for (AnnotationAttributes groupAttribute : groupAttributes) {
				AutumnApiHeaderParameterInfo headerParameter = new AutumnApiHeaderParameterInfo();
				headerParameter.setName(groupAttribute.getString(ApiHeaderParameter.FILED_NAME));
				headerParameter.setDataType(groupAttribute.getString(ApiHeaderParameter.FILED_DATA_TYPE));
				headerParameter.setDescription(groupAttribute.getString(ApiHeaderParameter.FILED_DESCRIPTION));
				headerParameter.setRequired(annotationAttributes.getBoolean(ApiHeaderParameter.FILED_REQUIRED));
				list.add(headerParameter);
			}
		}
		return list;
	}

	/**
	 * 构建 API 分组信息
	 *
	 * @param annotationAttributes
	 *            注解属性
	 * @return API 分组信息
	 */
	@SuppressWarnings("unchecked")
	private static List<AutumnApiGroupInfo> buildGroupInfos(AnnotationAttributes annotationAttributes) {
		List<AutumnApiGroupInfo> list = new ArrayList<>();
		AnnotationAttributes[] groupAttributes = annotationAttributes
				.getAnnotationArray(EnableAutumnSwagger.FILED_GROUPS);
		if (groupAttributes != null && groupAttributes.length > 0) {
			for (AnnotationAttributes groupAttribute : groupAttributes) {
				AutumnApiGroupInfo group = new AutumnApiGroupInfo();
				group.setGroupName(groupAttribute.getString(ApiGroup.FILED_GROUP_NAME));
				Object annotation = groupAttribute.get(ApiGroup.FILED_ANNOTATION);
				String defaultAnnotationClassName = Annotation.class.getName();
				Class<Annotation> annotationClass = null;
				String annotationClassName = "";
				if (annotation != null) {
					annotationClass = (Class<Annotation>) annotation;
					annotationClassName = annotationClass.getName();
				}
				if (annotationClass != null && !defaultAnnotationClassName.equals(annotationClassName)) {
					group.setAnnotation(annotationClass);
				} else {
					group.setPackages(groupAttribute.getString(ApiGroup.FILED_PACKAGES));
				}
				list.add(group);
			}
		}
		return list;
	}

	/**
	 * 构建 API 信息
	 *
	 * @param annotationAttributes
	 *            注解属性
	 * @return API 信息
	 */
	private static AutumnApiInfo buildApiInfo(AnnotationAttributes annotationAttributes) {
		AutumnApiInfo api = new AutumnApiInfo();
		api.setEnableAuthorize(annotationAttributes.getBoolean(EnableAutumnSwagger.FILED_ENABLE_AUTHORIZE));
		api.setAuthorizeHeader(annotationAttributes.getString(EnableAutumnSwagger.FILED_AUTHORIZE_HEADER));
		api.setTitle(annotationAttributes.getString(EnableAutumnSwagger.FILED_TITLE));
		api.setDescription(annotationAttributes.getString(EnableAutumnSwagger.FILED_DESCRIPTION));
		api.setVersion(annotationAttributes.getString(EnableAutumnSwagger.FILED_VERSION));
		api.setLicense(annotationAttributes.getString(EnableAutumnSwagger.FILED_LICENSE));
		api.setLicenseUrl(annotationAttributes.getString(EnableAutumnSwagger.FILED_LICENSE_URL));
		api.setAuthorName(annotationAttributes.getString(EnableAutumnSwagger.FILED_AUTHOR_NAME));
		api.setAuthorUrl(annotationAttributes.getString(EnableAutumnSwagger.FILED_AUTHOR_URL));
		api.setAuthorEmail(annotationAttributes.getString(EnableAutumnSwagger.FILED_AUTHOR_EMAIL));

		List<AutumnApiGroupInfo> apiGroups = buildGroupInfos(annotationAttributes);
		List<AutumnApiHeaderParameterInfo> apiHeaderParameters = buildHeaderParameters(annotationAttributes);
		api.setGroups(apiGroups);
		api.setHeaderParameters(apiHeaderParameters);
		return api;
	}

	/**
	 * 初始化 Swagger 文档信息
	 *
	 * @param annotationAttributes
	 *            配置
	 */
	public static void initialization(AnnotationAttributes annotationAttributes) {
		SWAGGER_API_INFO = buildApiInfo(annotationAttributes);
	}
}
